package cn.buildoncloud.labor.workermanapplet.business.modules.controller;

import cn.buildoncloud.labor.workermanapplet.business.modules.services.ProjectService;
import cn.buildoncloud.labor.workermanapplet.common.Constant;
import cn.buildoncloud.labor.workermanapplet.common.base.Result;
import cn.buildoncloud.labor.workermanapplet.common.page.Pagination;
import org.nutz.dao.entity.Record;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 项目管理
 * Description: cn.buildoncloud.labor.workermanapplet.business.modules.controller
 * Created by CuiZibo on 2018/11/5 10:47
 */
@RestController
@RequestMapping("/project")
public class ProjectController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ProjectService projectService;

    /**
     * 通过用户ID查询用户权限范围内的项目列表
     * @param userId
     * @return
     */
    @RequestMapping("/listByCompany")
    public Object listByCompany(@RequestParam(value = "userId", required = true) String userId) {
        try {
            List<Record> records = projectService.listByCompany(userId);

            return Result.success(Constant.API_OPERATION_SUCCESS, records);
        } catch (Exception e) {
            log.error("project/listByCompany: ", e);
            return Result.error(Constant.API_OPERATION_FAILED);
        }

    }

    /**
     * 通过用户ID查询用户权限范围内的项目列表
     * @param userId
     * @return
     */
    @RequestMapping("/listByCompanyNew")
    public Object listByCompanyNew(@RequestParam(value = "userId", required = true) String userId,
                                   @RequestParam(defaultValue = "1") Integer pageNumber,
                                   @RequestParam(defaultValue = "10") int pageSize) {
        try {
            Pagination pagination = projectService.listByUser(userId,pageNumber,pageSize);

            return Result.success(Constant.API_OPERATION_SUCCESS, pagination);
        } catch (Exception e) {
            log.error("project/listByCompany: ", e);
            return Result.error(Constant.API_OPERATION_FAILED);
        }

    }

}
